package cn.edu.zafu.bigdata.lwg;

import java.util.Scanner;

public class StudentManagementSystem {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("学生信息管理系统");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.out.println("退出程序");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        DatabaseManager.addStudent(student);
        System.out.println("学生信息添加成功！");
    }

    private static void searchStudent() {
        while (true) {
            System.out.print("请输入学号进行检索: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = DatabaseManager.getStudent(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入学号进行更新: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = DatabaseManager.getStudent(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                DatabaseManager.updateStudent(student);
                System.out.println("学生信息更新成功！");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入学号进行删除: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = DatabaseManager.getStudent(id);
            if (student != null) {
                DatabaseManager.deleteStudent(id);
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }
}
